package org.scmpatrol.scm.diff;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Lob;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
@Table(name = "DiffElement")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.CHAR)
@DiscriminatorValue(value="0")
public abstract class DiffElement {
	/**
	 * the internal id
	 */
	@Id
	@GeneratedValue
	protected Long id;

	private DiffElementType elementType;

	/**
	 * The start position in the original file this diff element is applied
	 */
	protected Integer startPosition;

	/**
	 * The modification content
	 */
	@Lob
	protected String content;

	protected DiffElement(DiffElementType elementType, Integer startPosition,
			String content) {
		this.elementType = elementType;
		this.startPosition = startPosition;
		this.content = content;
	}

	public DiffElementType getType() {
		return elementType;
	}

	public Integer getStartPosition() {
		return startPosition;
	}

	public String getContent() {
		return content;
	}

	public Integer getLength() {
		return content.length();
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

}
